package cate.game.pvp.throne.data.ko;

import cate.common.table.d.GDThrone;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("王座争霸-晋级赛数据")
public class TBKoAdvance {

	@NoteField(value = "胜者组败者组数据",detail = "Map<分组类型,分组数据>")
	public Map<Integer, TBKoAdvanceGroup> groups;

	public void initialize() {
		if (groups == null) {
			groups = new HashMap<>();
		}
		groups.values().forEach(TBKoAdvanceGroup::initialize);
		buildEmptyGroup();
	}


	private void buildEmptyGroup() {
		if (!groups.containsKey(GDThrone.KoGroupType.WINNER)) {
			buildGroup(GDThrone.KoGroupType.WINNER, new ArrayList<>());
		}
		if (!groups.containsKey(GDThrone.KoGroupType.LOSER)) {
			buildGroup(GDThrone.KoGroupType.LOSER, new ArrayList<>());
		}
	}

	@JsonIgnore
	public TBKoAdvanceGroup getGroup(int groupType){
		return groups.get(groupType);
	}

	public void clear() {
		groups.values().forEach(TBKoAdvanceGroup::clear);
	}


	public void buildGroup(int groupType, List<TBKoRankItem> rankList) {
		TBKoAdvanceGroup group = new TBKoAdvanceGroup(groupType);
		group.initialize();
		group.rank.record(rankList);
		groups.put(groupType, group);
	}

	public TBKoMatch findMatch(int groupType, int round, int matchGroup) {
		TBKoAdvanceGroup advanceGroupSPO = groups.get(groupType);
		if (advanceGroupSPO == null) {
			return null;
		}
		TBKoRound roundSPO = advanceGroupSPO.findRound(round);
		if (roundSPO == null) {
			return null;
		}
		return roundSPO.findMatch(matchGroup);
	}

	public List<TBKoReport> findRoleReports(String roleUid) {
		List<TBKoReport> results = new ArrayList<>();
		for (TBKoAdvanceGroup value : groups.values()) {
			for (TBKoRound round : value.rounds) {
				for (TBKoMatch match : round.matchs) {
					if (match.containRole(roleUid)) {
						TBKoReport report = TBKoReport.of(round.round, match);
						report.groupType = value.groupType;
						results.add(report);
					}
				}
			}
		}
		return results;
	}
}
